package com.zimmur.app.mocolink.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zimmur.app.mocolink.dao.AdminMapper;
import com.zimmur.app.mocolink.dao.AdminRoleMapper;
import com.zimmur.app.mocolink.domain.common.Constant;
import com.zimmur.app.mocolink.domain.form.AdminForm;
import com.zimmur.app.mocolink.domain.model.Admin;
import com.zimmur.app.mocolink.domain.model.AdminExample;
import com.zimmur.app.mocolink.domain.model.AdminRoleExample;
import com.zimmur.app.mocolink.domain.param.AdminParam;
import com.zimmur.app.mocolink.domain.util.DateUtils;
import com.zimmur.app.mocolink.domain.util.MD5;
import com.zimmur.app.mocolink.domain.util.PageUtil;
import com.zimmur.app.mocolink.domain.util.StringUtils;
import com.zimmur.app.mocolink.domain.vo.AdminInfo;
import com.zimmur.app.mocolink.service.IAdminService;

@Service
public class AdminServiceImpl implements IAdminService {
 
	@Autowired
	private AdminMapper adminMapper;
	@Autowired
	private AdminRoleMapper adminRoleMapper;

	@Override
	public Admin selectById(int id) {
		return adminMapper.selectByPrimaryKey(id);
	}

	@Override
	public Admin selectByAccount(String accountName) {
		AdminExample example = new AdminExample();
		example.createCriteria().andAccountNameEqualTo(accountName);
		List<Admin> admins = adminMapper.selectByExample(example);
		if(admins!=null && admins.size()>0){
			return admins.get(0);
		}
		return null;
	}

	@Override
	public int update(Admin admin) {
		
		return adminMapper.updateByPrimaryKeySelective(admin);
	}

	@Override
	public PageUtil<AdminInfo> selectByParam(AdminParam param) {
		PageUtil<AdminInfo> page = new PageUtil<>();
		List<AdminInfo> list=adminMapper.selectByParam(param) ;
		int count =adminMapper.countByParam(param);
		page.setPageNo(param.getPageNo());
		page.setPageSize(param.getPageSize());
		page.setItemList(list);
		page.setTotalRecord(count);
		return page;
	}
	@Transactional
	@Override
	public int saveAdmin(AdminForm form) {
		Integer id = form.getId()==null?0:form.getId();
		AdminExample example = new AdminExample();
		example.createCriteria().andAccountNameEqualTo(form.getAccountName()).andIdNotEqualTo(id);
		int count = adminMapper.countByExample(example);
		if(count>0){
			return -1;
		}
		Admin admin = new Admin();
		admin.setAccountName(form.getAccountName());
		admin.setEatablishTime(DateUtils.getNow());
		admin.setEatablishUser(form.getAdminUser());
		admin.setEmail(form.getEmail());
		admin.setIsEnabled(form.getIsEnabled());
		admin.setPhone(form.getPhone());
		admin.setPassword(form.getPassword());
		admin.setRealName(form.getRealName());
		int flag =0;
		if(id>0){
			admin.setId(id);
			flag = adminMapper.updateByPrimaryKeySelective(admin);
			adminRoleMapper.deleteByAdminId(id);
		}else{
			flag = adminMapper.insertSelective(admin);
			if(flag>0){
				id=admin.getId();
			}
		}
		if(!StringUtils.isBlank(form.getRoleIds())){
			String[] roleIdArray = form.getRoleIds().split(",");
			List<Integer> roleIdList = new ArrayList<>();
			for(String roleId:roleIdArray){
				roleIdList.add(Integer.valueOf(roleId));
			}
			if(roleIdList.size()>0){
				adminRoleMapper.batchInsert(roleIdList, id);
			}
		}
		return flag;
	}
	@Transactional
	@Override
	public int deleteAdmin(Integer adminId) {
		int flag = adminMapper.deleteByPrimaryKey(adminId);
		if(flag>0){
			adminRoleMapper.deleteByAdminId(adminId);
		}
		return flag;
	}

	@Override
	public int updateStatus(Integer adminId,Integer status,String adminName){
		Admin admin = adminMapper.selectByPrimaryKey(adminId);
		if(admin == null){
			return -1;
		}
		admin.setEatablishUser(adminName);
		admin.setEatablishTime(DateUtils.getNow());
		if(status == 0){
			admin.setIsEnabled(Constant.short_0);
		}else{
			admin.setIsEnabled(Constant.short_1);
		}
		return adminMapper.updateByPrimaryKeySelective(admin);
	}
}
